package com.cunjin.grade.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cunjin.base.service.impl.BaseServiceImpl;
import com.cunjin.grade.dao.GradeDao;
import com.cunjin.grade.entity.GradeEntity;
import com.cunjin.grade.service.GradeService;

import tk.mybatis.mapper.entity.Example;

@Service
public class GradeServiceImpl extends BaseServiceImpl<GradeEntity> implements GradeService {

	@Autowired
	private GradeDao gradeDao;
	
	@Override
	public List<GradeEntity> getcommoBox() {
		
		List<GradeEntity> selectAll =null;
		try {
			selectAll = gradeDao.selectAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return selectAll;
	}

	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> queryByPage(int page, int rows, String searchName) {
		
		//将分页的数据保存在map中
		Map<String, Object> pages=new HashMap<String, Object>();
		
		int before=(page-1)*rows;
		int after=rows;
		pages.put("before", before);
		pages.put("after", after);
		if(null !=searchName){
			pages.put("searchName", searchName+"%");
		}else{
			pages.put("searchName", null);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<GradeEntity> ls=gradeDao.myqueryByPage(pages);
			map.put("total",gradeDao.findAllByName(pages));
			map.put("rows",ls);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Map<String, Object> insert(GradeEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			//先判断是否重复
			String gradeName = entity.getName();
			Example example=new Example(GradeEntity.class);
			example.createCriteria().andCondition("TEACHER="+gradeName);
			List<GradeEntity> selectByExample = gradeDao.selectByExample(example);
			int size = selectByExample.size();
			//size==0说明没有个班级
			if(size==0){
				int count = gradeDao.insert(entity);
				if(count==1){
					map.put("code", 200);
					map.put("msg","添加班级成功");
				}
				else{
					map.put("code", 500);
				}
			}else{
				map.put("code", 201);
				map.put("msg","班级已经存在");
			}
		} catch (Exception e) {
			map.put("code", 500);
			e.printStackTrace();
		}
		return map;
	}

	
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> deleteByids(String[] split) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			
			int cs=0;
			for (String s : split) {
				int did = Integer.parseInt(s);
				gradeDao.deleteByPrimaryKey(did);
				cs++;
			}
			map.put("code", 200);
			map.put("cs", cs);
		} catch (Exception e) {
			map.put("code",500);
			e.printStackTrace();
		}
		return map;
	}
	
	@Override
	public Map<String, Object> update(GradeEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		//排除更新的宿舍重复性
		Integer oldId = entity.getId();
		String oldname = entity.getName();
		GradeEntity oldentity = gradeDao.selectByPrimaryKey(oldId);
		String oldgrade = oldentity.getName();
		//如果是两个相等没有修改宿舍名字
		if(oldname.equals(oldgrade)){
			gradeDao.updateByPrimaryKeySelective(entity);
			map.put("code", 200);
			map.put("msg","更新成功");
			return map;
		}else{
			//修改了宿舍名称
			try {
				GradeEntity selectByName = gradeDao.selectByName(oldname);
				//==null 说明没有这个宿舍
				if(null ==selectByName){
					gradeDao.updateByPrimaryKeySelective(entity);
				}else{
					map.put("code", 201);
					map.put("msg", "该班级"+oldname+"已经存在");
					return map;
				}
			} catch (Exception e) {
				map.put("code", 500);
				map.put("msg","更新失败");
				e.printStackTrace();
				return map;
			}
		}
		return map;
	}
}
